home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet internetowy / Rozne / HTTrack 3.40-2 / httrack-3.40-2.exe / {app} / src / htstools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-01-21  |  27.4 KB  |  1,041 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       various tools (filename analyzing ..)                  */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. /* Internal engine bytecode */
  39. #define HTS_INTERNAL_BYTECODE
  40.  
  41. #include "htstools.h"
  42.  
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include <ctype.h>
  46. /* String */
  47. #include "htsstrings.h"
  48. /* END specific definitions */
  49.  
  50.  
  51. // forme α partir d'un lien et du contexte (origin_fil et origin_adr d'o∙ il est tirΘ) adr et fil
  52. // [adr et fil sont des buffers de 1ko]
  53. // 0 : ok
  54. // -1 : erreur
  55. // -2 : protocole non supportΘ (ftp)
  56. int ident_url_relatif(char *lien,char* origin_adr,char* origin_fil,char* adr,char* fil) {
  57.   int ok=0;
  58.   int scheme=0;
  59.  
  60.   adr[0]='\0'; fil[0]='\0';    //effacer buffers
  61.  
  62.   // lien non vide!
  63.   if (strnotempty(lien)==0) return -1;    // erreur!
  64.  
  65.   // Scheme?
  66.   {
  67.     char* a=lien;
  68.     while (isalpha((unsigned char)*a))
  69.       a++;
  70.     if (*a == ':')
  71.       scheme=1;
  72.   }
  73.  
  74.   // filtrer les parazites (mailto & cie)
  75.   // scheme+authority (//)
  76.   if (
  77.                (strfield(lien,"http://"))        // scheme+//
  78.             || (strfield(lien,"file://"))   // scheme+//
  79.             || (strncmp(lien,"//",2)==0)    // // sans scheme (-> default)
  80.        ) {
  81.     if (ident_url_absolute(lien,adr,fil)==-1) {        
  82.       ok=-1;    // erreur URL
  83.     }
  84.   }
  85.   else if (strfield(lien,"ftp://")) {
  86.     // Note: ftp:foobar.gif is not valid
  87.     if (ftp_available()) {     // ftp supportΘ
  88.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  89.         ok=-1;    // erreur URL
  90.       }
  91.     } else {
  92.       ok=-2;  // non supportΘ
  93.     }
  94. #if HTS_USEMMS
  95.     } else if (strfield(lien,"mms://")) {
  96.         if (ident_url_absolute(lien,adr,fil)==-1) {        
  97.             ok=-1;    // erreur URL
  98.         }
  99. #endif
  100. #if HTS_USEOPENSSL
  101.   } else if (strfield(lien,"https://")) {
  102.     if (SSL_is_available) {
  103.       // Note: ftp:foobar.gif is not valid
  104.       if (ident_url_absolute(lien,adr,fil)==-1) {        
  105.         ok=-1;    // erreur URL
  106.       }
  107.     } else {
  108.       ok=-1;
  109.     }
  110. #endif
  111.   } else if ((scheme) && (
  112.     (!strfield(lien,"http:"))
  113.     && (!strfield(lien,"https:"))
  114.     && (!strfield(lien,"ftp:"))
  115. #if HTS_USEMMS
  116.     && (!strfield(lien,"mms:"))
  117. #endif
  118.     )) {
  119.     ok=-1;      // unknown scheme
  120.   } else {    // c'est un lien relatif
  121.     char* a;
  122.     
  123.     // On forme l'URL complΦte α partie de l'url actuelle
  124.     // et du chemin actuel si besoin est.
  125.     
  126.     // copier adresse
  127.     if (((int) strlen(origin_adr)<HTS_URLMAXSIZE) && ((int) strlen(origin_fil)<HTS_URLMAXSIZE) && ((int) strlen(lien)<HTS_URLMAXSIZE)) {
  128.  
  129.       /* patch scheme if necessary */
  130.       if (strfield(lien,"http:")) {
  131.         lien+=5;
  132.         strcpybuff(adr, jump_protocol(origin_adr));    // mΩme adresse ; protocole vide (http)
  133.       } else if (strfield(lien,"https:")) {
  134.         lien+=6;
  135.         strcpybuff(adr, "https://");   // mΩme adresse forcΘe en https
  136.         strcatbuff(adr, jump_protocol(origin_adr));
  137.       } else if (strfield(lien,"ftp:")) {
  138.         lien+=4;
  139.         strcpybuff(adr, "ftp://");   // mΩme adresse forcΘe en ftp
  140.         strcatbuff(adr, jump_protocol(origin_adr));
  141. #if HTS_USEMMS
  142.       } else if (strfield(lien,"mms:")) {
  143.         lien+=4;
  144.         strcpybuff(adr, "mms://");   // mΩme adresse forcΘe en ftp
  145.         strcatbuff(adr, jump_protocol(origin_adr));
  146. #endif
  147.       } else {
  148.         strcpybuff(adr,origin_adr);    // mΩme adresse ; et mΩme Θventuel protocole
  149.       }
  150.       
  151.       if (*lien!='/') {  // sinon c'est un lien absolu
  152.         if (*lien == '\0') {
  153.           strcpybuff(fil,origin_fil);
  154.         } else if (*lien == '?') {     // example: a href="?page=2"
  155.           char* a;
  156.           strcpybuff(fil,origin_fil);
  157.           a=strchr(fil,'?');
  158.           if (a) *a='\0';
  159.           strcatbuff(fil,lien);
  160.         } else {
  161.           a=strchr(origin_fil,'?');
  162.           if (a == NULL) a=origin_fil+strlen(origin_fil);
  163.           while((*a!='/') && ( a > origin_fil) ) a--;
  164.           if (*a=='/') {    // ok on a un '/'
  165.             if ( (((int) (a - origin_fil))+1+strlen(lien)) < HTS_URLMAXSIZE) {
  166.               // copier chemin
  167.               strncpy(fil,origin_fil,((int) (a - origin_fil))+1);
  168.               *(fil + ((int) (a - origin_fil))+1)='\0';
  169.               
  170.               // copier chemin relatif
  171.               if (((int) strlen(fil)+(int) strlen(lien)) < HTS_URLMAXSIZE) {
  172.                 strcatbuff(fil,lien + ((*lien=='/')?1:0) );      
  173.                 // simplifier url pour les ../
  174.                 fil_simplifie(fil);
  175.               } else
  176.                 ok=-1;    // erreur
  177.             } else {    // erreur
  178.               ok=-1;    // erreur URL
  179.             }
  180.           } else {    // erreur
  181.             ok=-1;    // erreur URL
  182.           }
  183.         }
  184.       } else { // chemin absolu
  185.         // copier chemin directement
  186.         strcatbuff(fil,lien);      
  187.         fil_simplifie(fil);
  188.       }  // *lien!='/'
  189.     } else
  190.       ok=-1;
  191.     
  192.   }  // test news: etc.
  193.  
  194.   // case insensitive pour adresse
  195.   {
  196.     char *a=jump_identification(adr);
  197.     while(*a) {
  198.       if ((*a>='A') && (*a<='Z'))
  199.         *a+='a'-'A';       
  200.       a++;
  201.     }
  202.   }
  203.   
  204.   return ok;
  205. }
  206.  
  207.  
  208.  
  209.  
  210.  
  211. // crΘer dans s, α partir du chemin courant curr_fil, le lien vers link (absolu)
  212. // un ident_url_relatif a dΘja ΘtΘ fait avant, pour que link ne soit pas un chemin relatif
  213. int lienrelatif(char* s,char* link,char* curr_fil) {
  214.   char BIGSTK _curr[HTS_URLMAXSIZE*2];
  215.   char BIGSTK newcurr_fil[HTS_URLMAXSIZE*2],newlink[HTS_URLMAXSIZE*2];
  216.   char* curr;
  217.   //int n=0;
  218.   char* a;
  219.   int slash=0;
  220.   //
  221.   newcurr_fil[0]='\0'; newlink[0]='\0';
  222.   //
  223.  
  224.   // patch: Θliminer les ? (paramΦtres) sinon bug
  225.   if ( (a=strchr(curr_fil,'?')) ) {
  226.     strncatbuff(newcurr_fil,curr_fil,(int) (a - curr_fil));
  227.     curr_fil = newcurr_fil;
  228.   }
  229.   if ( (a=strchr(link,'?')) ) {
  230.     strncatbuff(newlink,link,(int) (a - link));
  231.     link = newlink;
  232.   }
  233.  
  234.   // recopier uniquement le chemin courant
  235.   curr=_curr;
  236.   strcpybuff(curr,curr_fil);
  237.   if ((a=strchr(curr,'?'))==NULL)  // couper au ? (params)
  238.     a=curr+strlen(curr)-1;         // pas de params: aller α la fin
  239.   while((*a!='/') && ( a> curr)) a--;       // chercher dernier / du chemin courant
  240.   if (*a=='/') *(a+1)='\0';                           // couper dernier /
  241.   
  242.   // "effacer" s
  243.   s[0]='\0';
  244.   
  245.   // sauter ce qui est commun aux 2 chemins
  246.   {
  247.     char *l,*c;
  248.     if (*link=='/') link++;  // sauter slash
  249.     if (*curr=='/') curr++;
  250.     l=link;
  251.     c=curr;
  252.     // couper ce qui est commun
  253.     while ((streql(*link,*curr)) && (*link!=0)) {link++; curr++; }
  254.     // mais on veut un rΘpertoirer entier!
  255.     // si on a /toto/.. et /toto2/.. on ne veut pas sauter /toto !
  256.     while(((*link!='/') || (*curr!='/')) && ( link > l)) { link--; curr--; }
  257.     //if (*link=='/') link++;
  258.     //if (*curr=='/') curr++;
  259.   }
  260.   
  261.   // calculer la profondeur du rΘpertoire courant et remonter
  262.   // LES ../ ONT ETE SIMPLIFIES
  263.   a=curr;
  264.   if (*a=='/') a++;
  265.   while(*a) if (*(a++)=='/') strcatbuff(s,"../");
  266.   //if (strlen(s)==0) strcatbuff(s,"/");
  267.  
  268.   if (slash) strcatbuff(s,"/");    // garder absolu!!
  269.   
  270.   // on est dans le rΘpertoire de dΘpart, copier
  271.   strcatbuff(s,link + ((*link=='/')?1:0) );
  272.  
  273.   /* Security check */
  274.   if (strlen(s) >= HTS_URLMAXSIZE)
  275.     return -1;
  276.  
  277.   // on a maintenant une chaine de la forme ../../test/truc.html  
  278.   return 0;
  279. }
  280.  
  281. /* Is the link absolute (http://www..) or relative (/bar/foo.html) ? */
  282. int link_has_authority(char* lien) {
  283.   char* a=lien;
  284.   if (isalpha((unsigned char)*a)) {
  285.     // Skip scheme?
  286.     while (isalpha((unsigned char)*a))
  287.       a++;
  288.     if (*a == ':')
  289.       a++;
  290.     else
  291.       return 0;
  292.   }
  293.   if (strncmp(a,"//",2) == 0)
  294.     return 1;
  295.   return 0;
  296. }
  297.  
  298. int link_has_authorization(char* lien) {
  299.   char* adr = jump_protocol(lien);
  300.   char* firstslash = strchr(adr, '/');
  301.   char* detect = strchr(adr, '@');
  302.   if (firstslash) {
  303.     if (detect) {
  304.       return (detect < firstslash);
  305.     }
  306.   } else {
  307.     return (detect != NULL);
  308.   }
  309.   return 0;
  310. }
  311.  
  312.  
  313. // conversion chemin de fichier/dossier vers 8-3 ou ISO9660
  314. void long_to_83(int mode,char* n83,char* save) {
  315.   n83[0]='\0';
  316.  
  317.   while(*save) {
  318.     char fn83[256],fnl[256];
  319.     int i=0;
  320.     fn83[0]=fnl[0]='\0';
  321.     while((save[i]) && (save[i]!='/')) { fnl[i]=save[i]; i++; }
  322.     fnl[i]='\0';
  323.     // conversion
  324.     longfile_to_83(mode,fn83,fnl);
  325.     strcatbuff(n83,fn83);
  326.  
  327.     save+=i;
  328.     if (*save=='/') { strcatbuff(n83,"/"); save++; }
  329.   }
  330. }
  331.  
  332.  
  333. // conversion nom de fichier/dossier isolΘ vers 8-3 ou ISO9660
  334. void longfile_to_83(int mode,char* n83,char* save) {
  335.   int i=0,j=0,max=0;
  336.   char nom[256];
  337.   char ext[256];
  338.   nom[0]=ext[0]='\0';
  339.   
  340.   switch(mode) {
  341.   case 1:
  342.     max=8;
  343.     break;
  344.   case 2:
  345.     max=31;
  346.     break;
  347.   default:
  348.     max=8;
  349.     break;
  350.   }
  351.  
  352.   /* No starting . */
  353.   if (save[0] == '.') {
  354.     save[0]='_';
  355.   }
  356.   /* No multiple dots */
  357.   {
  358.     char* last_dot=strrchr(save, '.');
  359.     char* dot;
  360.     while((dot=strchr(save, '.'))) {
  361.       *dot = '_';
  362.     }
  363.     if (last_dot) {
  364.       *last_dot='.';
  365.     }
  366.   }
  367.   /* 
  368.   Avoid: (ISO9660, but also suitable for 8-3)
  369.   (Thanks to jonat@cellcast.com for te hint)
  370.   /:;?\#*~
  371.   0x00-0x1f and 0x80-0xff
  372.   */
  373.   for(i = 0 ; save[i] != 0 ; i++) {
  374.     char a = save[i];
  375.     if (a >= 'a' && a <= 'z') {
  376.       a -= 'a' - 'A';
  377.     }
  378.     else if ( ! ( (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9') || a == '_' || a == '.') ) {
  379.       a = '_';
  380.     }
  381.     save[i] = a;
  382.   }
  383.  
  384.   i=j=0;
  385.   while((i<max) && (save[j]) && (save[j]!='.')) {
  386.     if (save[j]!=' ') {
  387.       nom[i]=save[j]; 
  388.       i++; 
  389.     } 
  390.     j++; 
  391.   }  // recopier nom
  392.   nom[i]='\0';
  393.   if (save[j]) {  // il reste au moins un point
  394.     i=strlen(save)-1;
  395.     while((i>0) && (save[i]!='.') && (save[i]!='/')) i--;    // rechercher dernier .
  396.     if (save[i]=='.') {  // point!
  397.       int j=0;
  398.       i++;
  399.       while((j<3) && (save[i]) ) { if (save[i]!=' ') { ext[j]=save[i]; j++; } i++; }
  400.       ext[j]='\0';
  401.     }
  402.   }
  403.   // corriger vers 8-3
  404.   n83[0]='\0';
  405.   strncatbuff(n83,nom,max);
  406.   if (strnotempty(ext)) {
  407.     strcatbuff(n83,".");
  408.     strncatbuff(n83,ext,3);    
  409.   }
  410. }
  411.  
  412. // Θcrire backblue.gif
  413. int verif_backblue(httrackp* opt,char* base) {
  414.   int* done;
  415.   int ret=0;
  416.   NOSTATIC_RESERVE(done, int, 1);
  417.   //
  418.   if (!base) {   // init
  419.     *done=0;
  420.     return 0;
  421.   }
  422.   if ( (!*done)
  423.     || (fsize(fconcat(base,"backblue.gif")) != HTS_DATA_BACK_GIF_LEN)) {
  424.     FILE* fp = filecreate(fconcat(base,"backblue.gif"));
  425.     *done=1;
  426.     if (fp) {
  427.       if (fwrite(HTS_DATA_BACK_GIF,HTS_DATA_BACK_GIF_LEN,1,fp) != HTS_DATA_BACK_GIF_LEN)
  428.         ret=1;
  429.       fclose(fp);
  430.       usercommand(opt,0,NULL,fconcat(base,"backblue.gif"),"","");
  431.     } else
  432.       ret=1;
  433.     //
  434.     fp = filecreate(fconcat(base,"fade.gif"));
  435.     if (fp) {
  436.       if (fwrite(HTS_DATA_FADE_GIF,HTS_DATA_FADE_GIF_LEN,1,fp) != HTS_DATA_FADE_GIF_LEN)
  437.         ret=1;
  438.       fclose(fp);
  439.       usercommand(opt,0,NULL,fconcat(base,"fade.gif"),"","");
  440.     } else
  441.       ret=1;
  442.   } 
  443.   return ret;
  444. }
  445.  
  446. // flag
  447. int verif_external(int nb,int test) {
  448.   int* status;
  449.   NOSTATIC_RESERVE(status, int, 2);
  450.   if (!test)
  451.     status[nb]=0;   // reset
  452.   else if (!status[nb]) {
  453.     status[nb]=1;
  454.     return 1;
  455.   }
  456.   return 0;
  457. }
  458.  
  459.  
  460. // recherche chaεne de type truc<espaces>=
  461. // renvoi dΘcalage α effectuer ou 0 si non trouvΘ
  462. /* SECTION OPTIMISEE:
  463. #define rech_tageq(adr,s) ( \
  464.   ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) ? \
  465.     ( (streql(*adr,*s)) ? \
  466.       (__rech_tageq(adr,s)) \
  467.       : 0 \
  468.     ) \
  469.     : 0\
  470.   )
  471. */
  472. /*
  473. HTS_INLINE int rech_tageq(const char* adr,const char* s) { 
  474.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  475.     if (streql(*adr,*s)) {                           // tester premier octet (optimisation)
  476.       return __rech_tageq(adr,s);
  477.     }
  478.   }
  479.   return 0;
  480. }
  481. */
  482. // DeuxiΦme partie
  483. HTS_INLINE int __rech_tageq(const char* adr,const char* s) { 
  484.   int p;
  485.   p=strfield(adr,s);
  486.   if (p) {
  487.     while(is_space(adr[p])) p++;
  488.     if (adr[p]=='=') {
  489.       return p+1;
  490.     }
  491.   }
  492.   return 0;
  493. }
  494.  
  495. HTS_INLINE int rech_tageq_all(const char* adr, const char* s) { 
  496.     int p;
  497.     char quot = 0;
  498.     const char *token = NULL;
  499.     int s_len = (int) strlen(s);
  500.     if (adr == NULL) {
  501.         return 0;
  502.     }
  503.     for(p = 0 ; adr[p] != 0 ; p++) {
  504.         if (quot == 0) {
  505.             if (adr[p] == '"' || adr[p] == '\'' ) {
  506.                 quot = adr[p];
  507.             } else if (adr[p] == '=' || is_realspace(adr[p]) ) {
  508.                 token = NULL;
  509.             } else if (adr[p] == '>') {
  510.                 break;
  511.             } else {                /* note: bogus for bogus foo = bar */
  512.                 if (token == NULL) {
  513.                     if (strncasecmp(&adr[p], s, s_len) == 0 
  514.                         && (is_realspace(adr[p + s_len]) || adr[p + s_len] == '=')
  515.                         ) {
  516.                         for( p += s_len ; is_realspace(adr[p]) || adr[p] == '=' ; p++ );
  517.                         return p;
  518.                     }
  519.                     token = &adr[p];
  520.                 }
  521.             }
  522.         } else if (adr[p] == quot) {
  523.             quot = 0;
  524.         }
  525.     }
  526.   return 0;
  527. }
  528.  
  529. HTS_INLINE int rech_endtoken(const char* adr, const char** start) {
  530.   char quote = '\0';
  531.   int length = 0;
  532.   while(is_space(*adr)) adr++;
  533.   if (*adr == '"' || *adr == '\'') 
  534.     quote = *adr++;
  535.   *start = adr;
  536.   while(*adr != 0 && *adr != quote && (quote != '\0' || !is_space(*adr)) ) {
  537.     length++;
  538.     adr++;
  539.   }
  540.   return length;
  541. }
  542. // same, but check begining of adr wirh s (for <object src="bar.mov" .. hotspot123="foo.html">)
  543. HTS_INLINE int __rech_tageqbegdigits(const char* adr,const char* s) { 
  544.   int p;
  545.   p=strfield(adr,s);
  546.   if (p) {
  547.     while(isdigit((unsigned char)adr[p]))  p++;      // jump digits
  548.     while(is_space(adr[p])) p++;
  549.     if (adr[p]=='=') {
  550.       return p+1;
  551.     }
  552.   }
  553.   return 0;
  554. }
  555.  
  556. // tag sans =
  557. HTS_INLINE int rech_sampletag(const char* adr,const char* s) { 
  558.   int p;
  559.   if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  560.     p=strfield(adr,s);
  561.     if (p) {
  562.       if (!isalnum((unsigned char)adr[p])) {  // <srcbis n'est pas <src
  563.         return 1;
  564.       }
  565.       return 0;
  566.     }
  567.   }
  568.   return 0;
  569. }
  570.  
  571. // teste si le tag contenu dans from est Θgal α "tag"
  572. HTS_INLINE int check_tag(char* from,const char* tag) {
  573.   char* a=from+1;
  574.   int i=0;
  575.   char s[256];
  576.   while(is_space(*a)) a++;
  577.   while((isalnum((unsigned char)*a) || (*a=='/')) && (i<250)) { s[i++]=*a; a++; }
  578.   s[i++]='\0';
  579.   return (strfield2(s,tag));  // comparer
  580. }
  581.  
  582. // teste si un fichier dΘpasse le quota
  583. int istoobig(LLint size,LLint maxhtml,LLint maxnhtml,char* type) {
  584.   int ok=1;
  585.   if (size>0) {
  586.     if (is_hypertext_mime(type, "")) {
  587.       if (maxhtml>0) {
  588.         if (size>maxhtml)
  589.           ok=0;
  590.       }
  591.     } else {
  592.       if (maxnhtml>0) {
  593.         if (size>maxnhtml)
  594.           ok=0;
  595.       }
  596.     }
  597.   }
  598.   return (!ok);
  599. }
  600.  
  601.  
  602. static int sortTopIndexFnc(const void * a_, const void * b_) {
  603.   int cmp;
  604.   topindex_chain** a = (topindex_chain**) a_;
  605.   topindex_chain** b = (topindex_chain**) b_;
  606.   /* Category first, then name */
  607.   if ((cmp = (*a)->level - (*b)->level) == 0) {
  608.     if ((cmp = strcmpnocase((*a)->category, (*b)->category)) == 0) {
  609.       cmp = strcmpnocase((*a)->name, (*b)->name);
  610.     }
  611.   }
  612.   return cmp;
  613. }
  614.  
  615. HTSEXT_API char* hts_getcategory(char* filename);
  616.  
  617. HTSEXT_API int hts_buildtopindex(httrackp* opt,char* path,char* binpath) {
  618.   FILE* fpo;
  619.   int retval=0;
  620.   char BIGSTK rpath[1024*2];
  621.   char *toptemplate_header=NULL,*toptemplate_body=NULL,*toptemplate_footer=NULL,*toptemplate_bodycat=NULL;
  622.   
  623.   // et templates html
  624.   toptemplate_header=readfile_or(fconcat(binpath,"templates/topindex-header.html"),HTS_INDEX_HEADER);
  625.   toptemplate_body=readfile_or(fconcat(binpath,"templates/topindex-body.html"),HTS_INDEX_BODY);
  626.   toptemplate_bodycat=readfile_or(fconcat(binpath,"templates/topindex-bodycat.html"),HTS_INDEX_BODYCAT);
  627.   toptemplate_footer=readfile_or(fconcat(binpath,"templates/topindex-footer.html"),HTS_INDEX_FOOTER);
  628.   
  629.   if (toptemplate_header && toptemplate_body && toptemplate_footer && toptemplate_bodycat) {
  630.     
  631.     strcpybuff(rpath,path);
  632.     if (rpath[0]) {
  633.       if (rpath[strlen(rpath)-1]=='/')
  634.         rpath[strlen(rpath)-1]='\0';
  635.     }
  636.     
  637.     fpo=fopen(fconcat(rpath,"/index.html"),"wb");
  638.     if (fpo) {
  639.       String iname = STRING_EMPTY;
  640.       find_handle h;
  641.       verif_backblue(opt,concat(rpath,"/"));    // gΘnΘrer gif
  642.       // Header
  643.       fprintf(fpo,toptemplate_header,
  644.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  645.         );
  646.       
  647.       /* Find valid project names */
  648.       h = hts_findfirst(rpath);
  649.       if (h) {
  650.         struct topindex_chain * chain=NULL;
  651.         struct topindex_chain * startchain=NULL;
  652.         String iname = STRING_EMPTY;
  653.         int chainSize = 0;
  654.         do {
  655.           if (hts_findisdir(h)) {
  656.             StringStrcpy(iname,rpath);
  657.             StringStrcat(iname,"/");
  658.             StringStrcat(iname,hts_findgetname(h));
  659.             StringStrcat(iname,"/index.html");
  660.             if (fexist(StringBuff(iname))) {
  661.               int level = 0;
  662.               char* category = NULL;
  663.               struct topindex_chain * oldchain=chain;
  664.               
  665.               /* Check for an existing category */
  666.               StringStrcpy(iname,rpath);
  667.               StringStrcat(iname,"/");
  668.               StringStrcat(iname,hts_findgetname(h));
  669.               StringStrcat(iname,"/hts-cache/winprofile.ini");
  670.               if (fexist(StringBuff(iname))) {
  671.                 category = hts_getcategory(StringBuff(iname));
  672.                 if (category != NULL) {
  673.                   if (*category == '\0') {
  674.                     freet(category);
  675.                     category = NULL;
  676.                   }
  677.                 }
  678.               }
  679.               if (category == NULL) {
  680.                 category = strdupt("No categories");
  681.                 level = 1;
  682.               }
  683.  
  684.               chain=calloc(sizeof(struct topindex_chain), 1);
  685.               chainSize++;
  686.               if (!startchain) {
  687.                 startchain=chain;
  688.               }
  689.               if (chain) {
  690.                 if (oldchain) {
  691.                   oldchain->next=chain;
  692.                 }
  693.                 chain->next=NULL;
  694.                 strcpybuff(chain->name, hts_findgetname(h));
  695.                 chain->category = category;
  696.                 chain->level = level;
  697.               }
  698.             }
  699.             
  700.           }
  701.         } while(hts_findnext(h));
  702.         hts_findclose(h);
  703.         StringFree(iname);
  704.         
  705.         /* Sort chain */
  706.         {
  707.           struct topindex_chain** sortedElts = (struct topindex_chain**) calloct(sizeof(topindex_chain*), chainSize);
  708.           assertf(sortedElts != NULL);
  709.           if (sortedElts != NULL) {
  710.             int i;
  711.             char* category = "";
  712.             
  713.             /* Build array */
  714.             struct topindex_chain * chain = startchain;
  715.             for(i = 0 ; i < chainSize ; i++) {
  716.               assertf(chain != NULL);
  717.               sortedElts[i] = chain;
  718.               chain = chain->next;
  719.             }
  720.             qsort(sortedElts, chainSize, sizeof(topindex_chain*), sortTopIndexFnc);
  721.             
  722.             /* Build sorted index */
  723.             for(i = 0 ; i < chainSize ; i++) {
  724.               char BIGSTK hname[HTS_URLMAXSIZE*2];
  725.               strcpybuff(hname,sortedElts[i]->name);
  726.               escape_check_url(hname);
  727.  
  728.               /* Changed category */
  729.               if (strcmp(category, sortedElts[i]->category) != 0) {
  730.                 category = sortedElts[i]->category;
  731.                 fprintf(fpo,toptemplate_bodycat, category);
  732.               }
  733.               fprintf(fpo,toptemplate_body,
  734.                 hname,
  735.                 sortedElts[i]->name
  736.                 );
  737.             }
  738.             
  739.             /* Wipe elements */
  740.             for(i = 0 ; i < chainSize ; i++) {
  741.               freet(sortedElts[i]->category);
  742.               freet(sortedElts[i]);
  743.               sortedElts[i] = NULL;
  744.             }
  745.             freet(sortedElts);
  746.             
  747.             /* Return value */
  748.             retval=1;
  749.           }
  750.         }
  751.         
  752.       }
  753.       
  754.       // Footer
  755.       fprintf(fpo,toptemplate_footer,
  756.         "<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"
  757.         );
  758.       
  759.       fclose(fpo);
  760.       
  761.     }
  762.     
  763.   }
  764.  
  765.   if (toptemplate_header)
  766.     freet(toptemplate_header);
  767.   if (toptemplate_body)
  768.     freet(toptemplate_body);
  769.   if (toptemplate_footer)
  770.     freet(toptemplate_footer);
  771.   if (toptemplate_body)
  772.     freet(toptemplate_body);
  773.   
  774.   return retval;
  775. }
  776.  
  777. HTSEXT_API char* hts_getcategory(char* filename) {
  778.   String categ = STRING_EMPTY;
  779.   if (fexist(filename)) {
  780.     FILE* fp = fopen(filename, "rb");
  781.     if (fp != NULL) {
  782.       int done=0;
  783.       while(!feof(fp) && !done) {
  784.         char BIGSTK line[1024];
  785.         int n = linput(fp, line, sizeof(line) - 2);
  786.         if (n > 0) {
  787.           if (strfield(line, "category=")) {
  788.             unescapehttp(line+9, &categ);
  789.             done=1;
  790.           }
  791.         }
  792.       }
  793.       fclose(fp);
  794.     }
  795.   }
  796.   return StringBuff(categ);
  797. }
  798.  
  799. HTSEXT_API char* hts_getcategories(char* path, int type) {
  800.   String categ = STRING_EMPTY;
  801.   String profiles = STRING_EMPTY;
  802.   char* rpath = path;
  803.   find_handle h;
  804.   inthash hashCateg = NULL;
  805.   if (rpath[0]) {
  806.     if (rpath[strlen(rpath)-1]=='/') {
  807.       rpath[strlen(rpath)-1]='\0';      /* note: patching stored (inhash) value */
  808.     }
  809.   }
  810.   h = hts_findfirst(rpath);
  811.   if (h) {
  812.     struct topindex_chain * chain=NULL;
  813.     struct topindex_chain * startchain=NULL;
  814.     String iname = STRING_EMPTY;
  815.     if (type == 1) {
  816.       hashCateg = inthash_new(127);
  817.       StringStrcat(categ, "Test category 1");
  818.       StringStrcat(categ, "\r\nTest category 2");
  819.     }
  820.     do {
  821.       if (hts_findisdir(h)) {
  822.         char BIGSTK line2[1024];
  823.         StringStrcpy(iname,rpath);
  824.         StringStrcat(iname,"/");
  825.         StringStrcat(iname,hts_findgetname(h));
  826.         StringStrcat(iname,"/hts-cache/winprofile.ini");
  827.         if (fexist(StringBuff(iname))) {
  828.           if (type == 1) {
  829.             FILE* fp = fopen(StringBuff(iname), "rb");
  830.             if (fp != NULL) {
  831.               int done=0;
  832.               while(!feof(fp) && !done) {
  833.                 int n = linput(fp, line2, sizeof(line2) - 2);
  834.                 if (n > 0) {
  835.                   if (strfield(line2, "category=")) {
  836.                     if (*(line2+9)) {
  837.                       if (!inthash_read(hashCateg, line2+9, NULL)) {
  838.                         inthash_write(hashCateg, line2+9, 0);
  839.                         if (StringLength(categ) > 0) {
  840.                           StringStrcat(categ, "\r\n");
  841.                         }
  842.                         unescapehttp(line2+9, &categ);
  843.                       }
  844.                     }
  845.                     done=1;
  846.                   }
  847.                 }
  848.               }
  849.               line2[0] = '\0';
  850.               fclose(fp);
  851.             }
  852.           } else {
  853.             if (StringLength(profiles) > 0) {
  854.               StringStrcat(profiles, "\r\n");
  855.             }
  856.             StringStrcat(profiles, hts_findgetname(h));
  857.           }
  858.         }
  859.         
  860.       }
  861.     } while(hts_findnext(h));
  862.     hts_findclose(h);
  863.     StringFree(iname);
  864.   }
  865.   if (hashCateg) {
  866.     inthash_delete(&hashCateg);
  867.     hashCateg = NULL;
  868.   }
  869.   if (type == 1)
  870.     return StringBuff(categ);
  871.   else
  872.     return StringBuff(profiles);
  873. }
  874.  
  875.  
  876.  
  877.  
  878. // Portable directory find functions
  879. /*
  880. // Example:
  881. find_handle h = hts_findfirst("/tmp");
  882. if (h) {
  883.   do {
  884.     if (hts_findisfile(h))
  885.       printf("File: %s (%d octets)\n",hts_findgetname(h),hts_findgetsize(h));
  886.     else if (hts_findisdir(h))
  887.       printf("Dir: %s\n",hts_findgetname(h));
  888.   } while(hts_findnext(h));
  889.   hts_findclose(h);
  890. }
  891. */
  892. HTSEXT_API find_handle hts_findfirst(char* path) {
  893.   if (path) {
  894.     if (strnotempty(path)) {
  895.       find_handle_struct* find = (find_handle_struct*) calloc(1,sizeof(find_handle_struct));
  896.       if (find) {
  897.         memset(find, 0, sizeof(find_handle_struct));
  898. #if HTS_WIN
  899.         {
  900.           char BIGSTK rpath[1024*2];
  901.           strcpybuff(rpath,path);
  902.           if (rpath[0]) {
  903.             if (rpath[strlen(rpath)-1]!='\\')
  904.               strcatbuff(rpath,"\\");
  905.           }
  906.           strcatbuff(rpath,"*.*");
  907.           find->handle = FindFirstFileA(rpath,&find->hdata);
  908.           if (find->handle != INVALID_HANDLE_VALUE)
  909.             return find;
  910.         }
  911. #else
  912.         strcpybuff(find->path,path);
  913.         {
  914.           if (find->path[0]) {
  915.             if (find->path[strlen(find->path)-1]!='/')
  916.               strcatbuff(find->path,"/");
  917.           }
  918.         }
  919.         find->hdir=opendir(path);
  920.         if (find->hdir != NULL) {
  921.           if (hts_findnext(find) == 1)
  922.             return find;
  923.         }
  924. #endif
  925.         free((void*)find);
  926.       }
  927.     }
  928.   }
  929.   return NULL;   
  930. }
  931.  
  932. HTSEXT_API int hts_findnext(find_handle find) {
  933.   if (find) {
  934. #if HTS_WIN
  935.     if ( (FindNextFileA(find->handle,&find->hdata)))
  936.       return 1;
  937. #else
  938.     memset(&(find->filestat), 0, sizeof(find->filestat));
  939.     if ((find->dirp=readdir(find->hdir)))
  940.       if (find->dirp->d_name)
  941.         if (!stat(concat(find->path,find->dirp->d_name),&find->filestat))
  942.           return 1;
  943. #endif
  944.   }
  945.   return 0;
  946. }
  947.  
  948. HTSEXT_API int hts_findclose(find_handle find) {
  949.   if (find) {
  950. #if HTS_WIN
  951.     if (find->handle) {
  952.       FindClose(find->handle);
  953.       find->handle=NULL;
  954.     }
  955. #else
  956.     if (find->hdir) {
  957.       closedir (find->hdir);
  958.       find->hdir=NULL;
  959.     }
  960. #endif
  961.     free((void*)find);
  962.   }
  963.   return 0;
  964. }
  965.  
  966. HTSEXT_API char* hts_findgetname(find_handle find) {
  967.   if (find) {
  968. #if HTS_WIN
  969.     return find->hdata.cFileName;
  970. #else
  971.     if (find->dirp)
  972.       return find->dirp->d_name;
  973. #endif
  974.   }
  975.   return NULL;
  976. }
  977.  
  978. HTSEXT_API int hts_findgetsize(find_handle find) {
  979.   if (find) {
  980. #if HTS_WIN
  981.     return find->hdata.nFileSizeLow;
  982. #else
  983.     return find->filestat.st_size;
  984. #endif
  985.   }
  986.   return -1;
  987. }
  988.  
  989. HTSEXT_API int hts_findisdir(find_handle find) {
  990.   if (find) {
  991.     if (!hts_findissystem(find)) {
  992. #if HTS_WIN
  993.       if (find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY)
  994.         return 1;
  995. #else
  996.       if (S_ISDIR(find->filestat.st_mode))
  997.         return 1;
  998. #endif
  999.     }
  1000.   }
  1001.   return 0;
  1002. }
  1003. HTSEXT_API int hts_findisfile(find_handle find) {
  1004.   if (find) {
  1005.     if (!hts_findissystem(find)) {
  1006. #if HTS_WIN
  1007.       if (!(find->hdata.dwFileAttributes  & FILE_ATTRIBUTE_DIRECTORY))
  1008.         return 1;
  1009. #else
  1010.       if (S_ISREG(find->filestat.st_mode))
  1011.         return 1;
  1012. #endif
  1013.     }
  1014.   }
  1015.   return 0;
  1016. }
  1017. HTSEXT_API int hts_findissystem(find_handle find) {
  1018.   if (find) {
  1019. #if HTS_WIN
  1020.     if (find->hdata.dwFileAttributes  & (FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_TEMPORARY))
  1021.       return 1;
  1022.     else if ( (!strcmp(find->hdata.cFileName,"..")) || (!strcmp(find->hdata.cFileName,".")) )
  1023.       return 1;
  1024. #else
  1025.     if (
  1026.       (S_ISCHR(find->filestat.st_mode))
  1027.       || 
  1028.       (S_ISBLK(find->filestat.st_mode))
  1029.       || 
  1030.       (S_ISFIFO(find->filestat.st_mode))
  1031.       || 
  1032.       (S_ISSOCK(find->filestat.st_mode))
  1033.       )
  1034.       return 1;
  1035.     else if ( (!strcmp(find->dirp->d_name,"..")) || (!strcmp(find->dirp->d_name,".")) )
  1036.       return 1;
  1037. #endif
  1038.   }
  1039.   return 0;
  1040. }
  1041.